package com.ruoyi.robot.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.robot.pojo.po.Essay;
import com.ruoyi.robot.pojo.dto.EssayDto;
import com.ruoyi.robot.service.EssayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xds
 * @since 2023-01-16
 */
@Api(tags = "文章接口")
@RestController
@RequestMapping("/robot/essay")
public class EssayController extends BaseController {

    private final EssayService essayService;

    @Autowired
    public EssayController(EssayService essayService) {
        this.essayService = essayService;
    }

    /**
     * 查询文章表列表
     */
    @ApiOperation("查询文章表列表")
    @GetMapping("/list")
    public TableDataInfo list(EssayDto essayDto) {
        startPage();
        List<Essay> list = essayService.selectEssayList(essayDto);
        return getDataTable(list);
    }

    /**
     * 导出文章表列表
     */
    @ApiOperation("导出文章表列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EssayDto essayDto) {
        List<Essay> list = essayService.selectEssayList(essayDto);
        ExcelUtil<Essay> util = new ExcelUtil<Essay>(Essay.class);
        util.exportExcel(response, list, "文章表数据");
    }

    /**
     * 获取文章表详细信息
     */
    @ApiOperation("获取文章表详细信息")
    @GetMapping(value = "/{essayId}")
    public AjaxResult getInfo(@PathVariable("essayId") Long essayId) {
        return AjaxResult.success(essayService.selectEssayByEssayId(essayId));
    }

    /**
     * 新增文章表
     */
    @ApiOperation("新增文章表")
    @PostMapping
    public AjaxResult add(@RequestBody EssayDto essayDto) {
        return toAjax(essayService.insertEssay(essayDto));
    }

    /**
     * 修改文章表
     */
    @ApiOperation("修改文章表")
    @PutMapping
    public AjaxResult edit(@RequestBody EssayDto essayDto) {
        return toAjax(essayService.updateEssay(essayDto));
    }

    /**
     * 删除指定文章
     *
     * @param essayId
     * @return
     */
    @ApiOperation("删除指定文章")
    @DeleteMapping("/essayId")
    public AjaxResult deleteEssay(@PathVariable("essayId") Long essayId) {
        return toAjax(essayService.deleteEssayByEssayId(essayId));
    }

    /**
     * 删除文章表
     */
    @ApiOperation("删除文章表")
    @DeleteMapping("/{essayIds}")
    public AjaxResult remove(@PathVariable Long[] essayIds) {
        return toAjax(essayService.deleteEssayByEssayIds(essayIds));
    }

    /**
     * @param file
     * @return
     */
    @PostMapping("/uploadPicture")
    public AjaxResult uploadPicture(@RequestParam(value = "file", required = false) @RequestPart("file") MultipartFile file) {
        return essayService.uploadPicture(file);
    }
}

